package com.example.myapplicationndk.viewmodel;

import android.os.Bundle;
import android.view.View;

import com.example.myapplicationndk.databinding.ActivityViewModelMainBinding;
import com.example.myapplicationndk.utils.Logd;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

public class ViewModelMainActivity extends AppCompatActivity {

    private MainModel viewModel;
    private final LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);

    ActivityViewModelMainBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_view_model_main);

        mBinding = ActivityViewModelMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        // 不带参数
//        viewModel = ViewModelProviders.of(this).get(MainModel.class);
        // 带参数
        viewModel = new ViewModelProvider(this, new MyViewModelFactory(0)).get(MainModel.class);

        mBinding.plusOneBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.addOne(); // vm
                mBinding.infoText.setText(viewModel.getCurrentName().getValue() + "");   // v

            }
        });

        viewModel.getCurrentName().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                Logd.Logd(viewModel.getCurrentName().getValue() + " ");
            }
        });


    }

}